package com.easy.service.impl;

import com.easy.common.service.RedisService;
import com.easy.service.SessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * 登录缓存操作
 * @author 风亦未止
 * @date 2021/9/20 18:45
 */
@Service
public class SessionServiceImpl implements SessionService {
    @Autowired
    private RedisService redisService;

    @Value("${redis.database}")
    private String REDIS_BASE;
    @Value("${redis.key.sessionKey}")
    private String SESSION_KEY;
    @Value("${redis.key.sessionId}")
    private String SESSION_ID;
    /**
     * 过期时间
     */
    @Value("${redis.expire.common}")
    private long expireTime;

    @Override
    public void setSession(String sessionId, Object o) {
        String key=REDIS_BASE+":"+SESSION_ID+":"+sessionId;
        redisService.set(key,o,expireTime);
    }

    @Override
    public Object getSession(String sessionId) {
        String key=REDIS_BASE+":"+SESSION_ID+":"+sessionId;
        return redisService.get(key);
    }

    /**
     * 储存用户登录session
     */
    @Override
    public void setSession(String sessionKey, String sessionId, Object o){
        String key=REDIS_BASE+":"+SESSION_KEY+":"+sessionKey+":"+SESSION_ID+":"+sessionId;
        redisService.set(key,o,expireTime);
    }

    /**
     * 获取session
     * @param sessionKey
     * @param sessionId
     * @return
     */
    @Override
    public Object getSession(String sessionKey, String sessionId){
        String key=REDIS_BASE+":"+SESSION_KEY+":"+sessionKey+":"+SESSION_ID+":"+sessionId;
        return redisService.get(key);
    }

}
